import pymysql
from pyecharts.charts import Bar
from pyecharts import options
db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "hhp20040512",
    database = "crawlerDemo",
    charset = "utf8"
)
cursor = db.cursor()
sql = "select avg(job_sal),job_place from job group by job_place order by avg(job_sal) asc limit 6"

sal = []
city = []
try:
    cursor.execute(sql)
    res = cursor.fetchall()
    for row in res:
        sal.append(row[0])
        city.append(row[1])
except:
    print("查询出问题了~")
finally:
    cursor.close()
    db.close()

bar = Bar()
#添加x轴数据
bar.add_xaxis(city)
#添加y轴数据
bar.add_yaxis("月平均薪水/元",sal)
bar.set_global_opts(title_opts=options.TitleOpts(title="平均工资最低的六个城市",subtitle="单位/元"))
bar.render("baravg.html")

